package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Depot;
import com.huayue.entity.Product;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 库存
 */
@Component
public class DepotDao implements BaseDao<Depot> {
    @Override
    public int insert(Depot entity) {
        return DBHelper.executeUpdate(
            "insert into depot (product_id, real_number, normal_number, unusual_number, warn_number, is_status) values (?, ?, ?, ?, ?, ?);",
            entity.getProduct_id(), entity.getReal_number(), entity.getNormal_number(), entity.getUnusual_number(),
            entity.getWarn_number(), entity.getIs_status());
    }

    @Override
    public int update(Depot entity) {
        return DBHelper.executeUpdate(
            "update depot set product_id=?, real_number=?, normal_number=?, unusual_number=?, warn_number=?, is_status=? where id=?;",
            entity.getProduct_id(), entity.getReal_number(), entity.getNormal_number(), entity.getUnusual_number(),
            entity.getWarn_number(), entity.getIs_status(), entity.getId());
    }

    // 添加库存
    public int updateDepot() {
        return DBHelper.executeUpdate("");
    }

    @Override
    public int delete(Depot entity) {
        return DBHelper.executeUpdate("update depot set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from depot where is_deleted=0");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        String sql = "select d.*,p.name pname,pt.name ptname,su.name suname from depot d "
            + "left join product p on d.product_id=p.id left join product_type pt on p.product_type_id=pt.id "
            + "left join supplier su on p.supplier_id=su.id where d.is_deleted=0 ";
        Product product = (Product)obj[0];
        if (product.getName() != null && !"".equals(product.getName())) {
            sql += " and p.name like '%" + product.getName() + "%' ";
        }
        if (product.getProduct_type_id() != null && product.getProduct_type_id() != 0) {
            sql += " and pt.id = " + product.getProduct_type_id();
        }
        if (product.getSupplier_id() != null && product.getSupplier_id() != 0) {
            sql += " and su.id=" + product.getSupplier_id();
        }
        return DBHelper.executeQuery(sql + " limit ?,?", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Depot entity) {
        return DBHelper.executeQuery(
            "select d.*,p.name pname from depot d left join product p on d.product_id=p.id where d.is_deleted=0 and d.id=?",
            entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql = "select count(*) counts from depot d "
            + "left join product p on d.product_id=p.id left join product_type pt on p.product_type_id=pt.id "
            + "left join supplier su on p.supplier_id=su.id where d.is_deleted=0 ";
        Product product = (Product)obj[0];
        if (product.getName() != null && !"".equals(product.getName())) {
            sql += " and p.name like '%" + product.getName() + "%' ";
        }
        if (product.getProduct_type_id() != null && product.getProduct_type_id() != 0) {
            sql += " and pt.id = " + product.getProduct_type_id();
        }
        if (product.getSupplier_id() != null && product.getSupplier_id() != 0) {
            sql += " and su.id=" + product.getSupplier_id();
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public List<Map<String, Object>> findNoDepot() {
        return DBHelper.executeQuery(
            "select id,name from product where (select count(1) as num from depot where product.id = depot.product_id and depot.is_deleted=0  ) = 0 and product.is_deleted=0");
    }

    public int addNumber(Depot depot) {
        return DBHelper.executeUpdate("UPDATE depot SET real_number = real_number+? WHERE depot.product_id=?",
            depot.getReal_number(), depot.getProduct_id());
    }
}
